<template>
    <div class="apiCacheContainer">
        <div class="searchContainer">
            <ServiceConfigSearch />
        </div>
        <div class="insertContainer">
            <el-button type="primary" plain size="small" icon="PlusOutlined" @click="addConfig()">添 加</el-button>
            <el-button type="primary" plain icon="PlusOutlined" @click="refreshConfig()" style="float: right">刷新配置</el-button>
        </div>
        <div>
            <el-table
                :data="configCenterDataList.dataList"
                style="width: 100%; margin-bottom: 20px; margin-top: 20px;"
                row-key="id"
                border
                :stripe="true"
                :show-overflow-tooltip="true"
                class="tableContainer"
                max-height="70vh"
            >
                <el-table-column prop="application" label="微服务ID" />
                <el-table-column prop="profile" label="微服务profile" />
                <el-table-column prop="key" label="键" />
                <el-table-column prop="value" label="值" />
                <el-table-column align="center">
                    <template #header>
                        <span>操 作</span>
                    </template>
                    <template #default="scope">
                        <el-button
                            size="small"
                            type="success"
                            @click="handleUpdate(scope.$index, scope.row)">修 改</el-button>
                        <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删 除</el-button>
                        <el-button type="primary" size="small" @click="handleCopy(scope.$index, scope.row)">复 制</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                v-model:current-page="configCenterDataList.pageIndex"
                v-model:page-size="configCenterDataList.pageSize"
                :small="false"
                :background="true"
                layout="prev, pager, next, jumper, sizes, total"
                :total="configCenterDataList.recordCount"
                @change="changePage"
                :page-sizes="[10, 20, 30, 50, 100, 200, 400]"
                style="float: right"
            />
        </div>

        <div>
            <ServiceAddOrUpdateConfigDialog :configCenterForm="configCenterForm"/>
        </div>
        <div>
            <ServiceDeleteConfigDialog :configCenterForm="configCenterForm"/>
        </div>
        <div>
            <ServiceRefreshConfigCenterDialog />
        </div>
    </div>
</template>

<script name="ServiceConfigManage" setup lang="ts">
import ServiceConfigSearch from "@/components/serviceManage/serviceConfigManage/ServiceConfigSearch.vue";
import ServiceAddOrUpdateConfigDialog from "@/components/serviceManage/serviceConfigManage/ServiceAddOrUpdateConfigDialog.vue";
import ServiceDeleteConfigDialog from "@/components/serviceManage/serviceConfigManage/ServiceDeleteConfigDialog.vue";
import ServiceRefreshConfigCenterDialog from "@/components/serviceManage/serviceConfigManage/ServiceRefreshConfigCenterDialog.vue";
import {configInfoQuery} from "@/request/configCenter";
import {createTag} from "@/utils/ywuCommon";
import {computed, ref} from "vue";
import {useServiceConfigStore} from "@/stores/serviceManage/serciceConfig";
import {useRoute} from "vue-router";

let configCenterForm = ref({
    id: '',
    application: '',
    profile: '',
    key: '',
    value: ''
});

let configCenterDataList = computed(() => {return useServiceConfigStore().configCenterDataList});
let configSearchData = computed(() => {return useServiceConfigStore().configCenterSearchData});

// 查询配置信息列表
configInfoQuery(configSearchData.value).then(res => {
    useServiceConfigStore().resetConfigCenterDataList(res.data);
})

// 创建标签
let route = useRoute();
createTag(route);

function addConfig() {
    configCenterForm.value = {
        id: '',
        application: '',
        profile: '',
        key: '',
        value: ''
    }
    useServiceConfigStore().resetAddOrUpdateDialog(true);
}

function handleUpdate(index, row) {
    configCenterForm.value = {
        id: row.id,
        application: row.application,
        profile: row.profile,
        key: row.key,
        value: row.value
    }
    useServiceConfigStore().resetAddOrUpdateDialog(true);
}

function handleDelete(index, row) {
    configCenterForm.value = {
        id: row.id,
        application: row.application,
        profile: row.profile,
        key: row.key,
        value: row.value
    }
    useServiceConfigStore().resetDeleteDialog(true);
}

function handleCopy(index, row) {
    configCenterForm.value = {
        id: '',
        application: row.application,
        profile: row.profile,
        key: row.key,
        value: row.value
    }
    useServiceConfigStore().resetAddOrUpdateDialog(true);
}

function changePage(index, size) {
    configSearchData.value["pageInfo"] = {
        pageIndex: index,
        pageSize: size
    };
    configInfoQuery(configSearchData.value).then(res => {
        useServiceConfigStore().resetConfigCenterDataList(res.data);
    })
}

function refreshConfig() {
    useServiceConfigStore().resetRefreshDialog(true);
}
</script>

<style scoped lang="less">
.apiCacheContainer {
    width: 98%;
    height: 100%;
    margin: 0.2%;
    .searchContainer {
        width: 100%;
        margin-top: 1%;
    }
    .tableContainer {
        margin-left: 1%;
    }
    .insertContainer {
        margin-left: 1%;
        margin-top: 0.7%;
    }
    .refreshContainer {
        margin-top: 5%;
        //去除光标
        caret-color: transparent;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
}
</style>